package com.bw.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bw.bean.DataBean;
import com.bw.service.DataBeanService;
import com.fasterxml.jackson.annotation.JsonAlias;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DataBeanController {

    @Autowired
    private DataBeanService dataBeanService;

    @RequestMapping("/s1")
    public JSONObject select1(){
        //对象  put 方法(key,value) key 必须和sugar中给的json格式的数据中的key保持一致
        JSONObject result = new JSONObject();
        result.put("status",0);
        result.put("msg","");

        //创建对象
        JSONObject data = new JSONObject();
        // 存入到result中
        result.put("data",data);

        //创建数组
        JSONArray categories = new JSONArray();
        JSONArray series = new JSONArray();
        //存入到data中
        data.put("categories",categories);
        data.put("series",series);

        //创建对象
        JSONObject series_obj1 = new JSONObject();
        //存入到到series中
        series.add(series_obj1);

        //设置key和value
        series_obj1.put("name","菜品活动优惠（下单）金额");
        //创建对象
        JSONArray obj1_data = new JSONArray();
        series_obj1.put("data",obj1_data);


        //执行业务查询
        List<DataBean> beans = dataBeanService.select1();
        for (DataBean bean : beans) {
            categories.add(bean.getName());// 菜品名称
            obj1_data.add(bean.getCta()); // 下单金额
        }


        return result;
    }


    @RequestMapping("/s2")
    public JSONObject select2(){
        JSONObject result = new JSONObject();
        result.put("status",0);
        result.put("msg","");

        JSONObject data = new JSONObject();
        result.put("data",data);

        //创建对象
        JSONArray columns = new JSONArray();
        JSONArray rows = new JSONArray();
        data.put("columns",columns);
        data.put("rows",rows);

        JSONObject col1 = new JSONObject();
        col1.put("id","cta");
        col1.put("name","下单金额");

        JSONObject col2 = new JSONObject();
        col2.put("id","ctb");
        col2.put("name","下单次数");

        JSONObject col3 = new JSONObject();
        col3.put("id","ctc");
        col3.put("name","下单人数");

        JSONObject col4 = new JSONObject();
        col4.put("id","ctd");
        col4.put("name","实收金额");


        JSONObject col5 = new JSONObject();
        col5.put("id","cte");
        col5.put("name","退款成功金额");

        columns.add(col1);
        columns.add(col2);
        columns.add(col3);
        columns.add(col4);
        columns.add(col5);

        List<DataBean> beans = dataBeanService.select2();

        for (DataBean bean : beans) {
            JSONObject row = new JSONObject();
            row.put("cta",bean.getCta());
            row.put("ctb",bean.getCtb());
            row.put("ctc",bean.getCtc());
            row.put("ctd",bean.getCtd());
            row.put("cte",bean.getCte());

            rows.add(row);
        }


        return result;
    }


    @RequestMapping("/s3")
    public JSONObject select3(){
        // put方法
        JSONObject result = new JSONObject();
        result.put("status",0);
        result.put("msg","");

        JSONObject data = new JSONObject();
        result.put("data",data);

        //创建数组对象
        JSONArray columns = new JSONArray();
        JSONArray rows = new JSONArray();

        data.put("columns",columns);
        data.put("rows",rows);

        // "店铺名称",评价次数,好评次数,平均评分,好评率
        JSONObject col1 = new JSONObject();
        col1.put("id","name");
        col1.put("name","店铺名称");

        JSONObject col2 = new JSONObject();
        col2.put("id","cta");
        col2.put("name","评价次数");

        JSONObject col3 = new JSONObject();
        col3.put("id","ctb");
        col3.put("name","好评次数");

        JSONObject col4 = new JSONObject();
        col4.put("id","ctc");
        col4.put("name","平均评分");

        JSONObject col5 = new JSONObject();
        col5.put("id","ctd");
        col5.put("name","好评率");

        //加入到列的数组中
        columns.add(col1);
        columns.add(col2);
        columns.add(col3);
        columns.add(col4);
        columns.add(col5);

        //从数据库中查询数据
        List<DataBean> beans = dataBeanService.select3();
        data.put("total",beans.size()); //记录数
        //遍历数据
        for (DataBean bean : beans) {
            //创建一行对象
            JSONObject row = new JSONObject();
            row.put("name",bean.getName()); //name对应列的值  name和col1中id名称一致
            row.put("cta",bean.getCta());  //cta 对应列的值   cta和col2中id名称一致
            row.put("ctb",bean.getCtb());
            row.put("ctc",bean.getCtc());
            row.put("ctd",bean.getCtd());

            //row对象加入到rows中
            rows.add(row);
        }


        return result;
    }


    @RequestMapping("/s4")
    public JSONObject select4(){
        //创建对象
        JSONObject result = new JSONObject();
        //存入相应的字段 status,msg,data  至于什么类型 就要根据你从sugar粘贴过来的数据 来进行处理
        result.put("status",0);
        result.put("msg","");

        JSONObject data = new JSONObject();
        result.put("data",data);

        //创建数组对象
        JSONArray categories = new JSONArray();
        JSONArray series = new JSONArray();
        //存入对象
        data.put("categories",categories);
        data.put("series",series);

        JSONObject series_obj1 = new JSONObject();
        series.add(series_obj1); //数组中add方法
        series_obj1.put("name","下单金额");
        //创建对象
        JSONArray series_obj1_data = new JSONArray();
        //存入对象
        series_obj1.put("data",series_obj1_data);




        JSONObject series_obj2 = new JSONObject();
        series.add(series_obj2); //数组中add方法
        series_obj2.put("name","下单次数");
        //创建对象
        JSONArray series_obj2_data = new JSONArray();
        //存入对象
        series_obj2.put("data",series_obj2_data);


        JSONObject series_obj3 = new JSONObject();
        series.add(series_obj3); //数组中add方法
        series_obj3.put("name","下单人数");
        //创建对象
        JSONArray series_obj3_data = new JSONArray();
        //存入对象
        series_obj3.put("data",series_obj3_data);


        //查询业务数据
        List<DataBean> beans = dataBeanService.select4();
        //遍历对象
        for (DataBean bean : beans) {
            categories.add(bean.getName());
            series_obj1_data.add(bean.getCta());
            series_obj2_data.add(bean.getCtb());
            series_obj3_data.add(bean.getCtc());
        }

        return result;
    }
}
